package ws;

import interfaces.ServiceInterface;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import model.BusInfo;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.gson.Gson;
import com.vividsolutions.jts.geom.Point;

import java.lang.reflect.Field;

public class Service implements ServiceInterface {
	
	private final String URL_DEFAULT = "http://www.etufor.ce.gov.br/sit_web/itinerarios.aspx";
	private final String LINE_URL = "http://www.etufor.ce.gov.br/sit_web/itinerarios_sp.aspx?cd_lin=";
	private final String CSS_QUERY_IDA = "table td:nth-child(2)";
	private final String CSS_QUERY_VOLTA = "table td:nth-child(3)";

	@Override
	public String extractLineRoute(int line) throws IOException {
		List<String> ruas = new ArrayList<String>();
		String url = LINE_URL + line;
		Document doc = Jsoup.connect(url).post();
		Elements ida = doc.select(CSS_QUERY_IDA);
		System.out.println("IDA");
		for (Element dest : ida) {
			String rua = dest.text();
			System.out.println(rua);
			ruas.add(rua);
		}
		Elements volta = doc.select(CSS_QUERY_VOLTA);
		System.out.println("VOLTA");
		for (Element dest : volta) {
			String rua = dest.text();
			System.out.println(rua);
			ruas.add(rua);
		}
		Gson g = new Gson();
		return g.toJson(ruas);
	}

	@Override
	public String genRouteByPoint(String routeByStreet) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveRoute(String routeByStreet) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public String getRoute(int line) {
		KmlOperation ko = new KmlOperation();
		Gson g = new Gson();
		return g.toJson(ko.getBusForCode(line).getDescription());
	}
	
	public String getAllLines() {
		KmlOperation ko = new KmlOperation();
		Gson g = new Gson();
		return g.toJson(ko.getAllLines());
	}

	public String getLinesCoordenates(double latiO, double longO, double latiF, double longF){
		List<Integer> result = new ArrayList<Integer>();
		KmlOperation ko = new KmlOperation();
		List<BusInfo> lines = ko.linesByPoint(latiO, longO, latiF, longF);
		
		for(BusInfo b : lines)
			result.add(b.getCod());
		
		Gson g = new Gson();
		return g.toJson(result);
	}
	
	public String getLinesTerminal(double latiO, double longO, double latiF, double longF){
		List<String> result = new ArrayList<String>();
		try {
			Class cls = Class.forName("model.Terminais");
			Field[] fl = cls.getDeclaredFields();
			for(int i=0;i<fl.length;i++){
				Point p = (Point)fl[i].get(null);	
				List<BusInfo> linesOT = BusInfo.getLinesNextCoordenate(latiO, longO, p.getY(), p.getX(),300,1);
				if(linesOT.size()>0){
					List<BusInfo> linesFT = BusInfo.getLinesNextCoordenate(p.getY(), p.getX(),latiF, longF,1,300);
					if(linesFT.size()>0){
						String rota = "Origem:";
						for(BusInfo b: linesOT){
							rota+=b.getCod()+","; 
						}
						rota = rota.substring(0, rota.length()-1)+";"+fl[i].getName()+":";
						for(BusInfo b: linesFT){
							rota+=b.getCod()+","; 
						}
						rota = rota.substring(0, rota.length()-1);
						System.out.println(rota);
						result.add(rota);
					}
				}
			}
			
		} catch (ClassNotFoundException | IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		Gson g = new Gson();
		return g.toJson(result);
	}
	
//	@Override
//	public String getAllLines() {
//		List<Line> lines = new ArrayList<Line>();
//		String allLines = null;
//		try {
//			Document doc = Jsoup.connect(URL_DEFAULT).data("cmbTipoConsulta","2").
//					data("__EVENTTARGET","").data("__EVENTARGUMENT","").
//					data("txtLin","").data("btnPesq","Pesquisar").post();
//			doc.outputSettings().escapeMode(EscapeMode.xhtml);
//			Elements select = doc.select("#cmbLinhas");
//			Element linhas = select.first();
//			for (Node linha : linhas.childNodes()){
//				String nomeLinha = linha.childNodes().toString();
//				String codigoLinha = linha.attr("value").toString();
//				if(!codigoLinha.equals("") && !codigoLinha.equals("0") && !nomeLinha.equals("[]")){
//					System.out.println(codigoLinha + " - " + nomeLinha);
//					Line l = new Line();
//					l.setCod(Integer.parseInt(codigoLinha));
//					l.setName(nomeLinha.substring(1,nomeLinha.length()-1));
////					DB.getSession();
////					DB.save(l);
//					lines.add(l);
//				}
//			}
//			Gson g= new Gson();
//			allLines = g.toJson(lines); 
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return allLines;
//	}

}
